package jp.tkym.labs.store;

class ValueConverterForDatastore{
	
	static boolean isStringValue(Class<?> type){
		if(type == String.class) return true;
		return false;
	}
	
	static boolean isLongValue(Class<?> type){
		if(type == Long.class || type == long.class) return true; 
		if(type == Integer.class || type == int.class) return true;
		if(type == Short.class || type == short.class) return true;
		if(type == Byte.class || type == byte.class) return true;
		return false;
	}
	
	static Object fromDatastore(Class<?> type, long value){
		if(type == Long.class || type == long.class) return value; 
		if(type == Integer.class || type == int.class) return (int) value;
		if(type == Short.class || type == short.class) return (short) value;
		if(type == Byte.class || type == byte.class) return (byte) value;
		throw new IllegalArgumentException(type.getName()+" is not supported");
	}
	
	static Object fromDatastore(Class<?> type, String value){
		if(type == String.class) return value;
		throw new IllegalArgumentException(type.getName()+" is not supported");
	}
	
	static Object fromDatastore(Class<?> type, Object value){
		if(isLongValue(type))
			return fromDatastore(type, ((Long) value).longValue());
		if(isStringValue(type))
			return fromDatastore(type, (String) value);
		throw new IllegalArgumentException(type.getName()+" is not supported");
	}
	
	static String toStringForDatastore(Class<?> type, Object value){
		if(type == String.class) return (String) value; 
			throw new IllegalArgumentException(type.getName()+" is not supported");
	}
	
	static long toLongForDatastore(Class<?> type, Object value){
		if(type == Long.class || type == long.class) 
			return (Long)value; 
		if(type == Integer.class || type == int.class) 
			return ((Integer)value).longValue(); 
		if(type == Short.class || type == short.class) 
			return ((Short)value).longValue(); 
		if(type == Byte.class || type == byte.class) 
			return ((Byte)value).longValue();
		throw new IllegalArgumentException(type.getName()+" is not supported");
	}
}